﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using famails.common;
using famails.dto;
using famails.App_Code.source.dto;

namespace famails.dao
{
    /// <summary>
    /// Summary description for ProductDTO
    /// </summary>
    public class CM_ProductDAO
    {
        public CM_ProductDAO()
        {

        }
        public DataTable GetByID(decimal ID, int UserID)
        {
            SqlCommand cmd = new SqlCommand("[SP_CM_Product_Get4Edit] "+ID+","+UserID, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetAll(int userid)
        {
            SqlCommand cmd = new SqlCommand("SELECT a.*, subName UserName, subEmail FROM tblCM_Products a left join tblsubclient s on s.subId = a.userid WHERE a.isdelete <> 1 and createby="+userid, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable Search(string Code, string Name,int userid)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Product_Search ", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = Name;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = Code;
            cmd.Parameters.Add("@userid", SqlDbType.Int).Value = userid;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
        public DataTable Search(string Name, string Code, int groupid, string from, string to, decimal pfrom, decimal pto, int userid)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Product_Search_Advance", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = Name;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = Code;
            cmd.Parameters.Add("@from", SqlDbType.NVarChar).Value = from;
            cmd.Parameters.Add("@to", SqlDbType.NVarChar).Value = to;
            cmd.Parameters.Add("@pfrom", SqlDbType.Decimal).Value = pfrom;
            cmd.Parameters.Add("@pto", SqlDbType.Decimal).Value = pto;
            cmd.Parameters.Add("@userid", SqlDbType.Int).Value = userid;
            cmd.Parameters.Add("@groupid", SqlDbType.Int).Value = groupid;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public object Insert(CM_ProductDTO dto)
        {
            SqlCommand cmd = new SqlCommand("insert into tblCM_Products(ProductName, ProductCode, Price, CreateDate, createBy, groupid)values(@name, @code, @price, getdate(), @createby, @groupid) select @@identity", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@price", SqlDbType.Decimal).Value = dto.Price;
            cmd.Parameters.Add("@CreateBy", SqlDbType.Int).Value = dto.CreateBy;
            cmd.Parameters.Add("@GroupID", SqlDbType.Int).Value = dto.GroupID;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dto.ProductName;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = dto.ProductCode;
            object i = cmd.ExecuteScalar();
            cmd.Dispose();
            return i;
        }

        public bool Edit(CM_ProductDTO dto)
        {
            SqlCommand cmd = new SqlCommand("update tblCM_Products set ProductName=@name, ProductCode=@code, groupid=@groupid, price=@price where Productid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = dto.ProductID;
            cmd.Parameters.Add("@price", SqlDbType.Decimal).Value = dto.Price;
            cmd.Parameters.Add("@CreateBy", SqlDbType.Int).Value = dto.CreateBy;
            cmd.Parameters.Add("@GroupID", SqlDbType.Int).Value = dto.GroupID;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dto.ProductName;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = dto.ProductCode;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        public bool Delete(int id)
        {
            SqlCommand cmd = new SqlCommand("update tblCM_Products set isdelete=1 where Productid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        internal bool CheckExistsName(string name, int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblCM_Products WHERE Productid <> @id and Productname = @name and isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count > 0;
        }

        internal bool CheckBeforeDelete(int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT Productid FROM tblCM_orderdetails d inner join tblCM_Orders o on o.orderid = d.orderid WHERE Productid = @id and isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count == 0;
        }

        internal DataTable GetByCode(string code, int id)
        {
            SqlCommand cmd = new SqlCommand("Select * from tblCM_Products where productcode='"+code+"' ", ConnectionData._MyConnection);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        internal bool CheckExistsCode(string code, decimal id, int userid)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Product_GetByCode '" + code + "', "+id+", "+userid, ConnectionData._MyConnection);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count>0;
        }

    }
}
